package com.masterclient.app.businessmodule.minemodule.address;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eoner.baselib.enums.FCPageViewState;
import com.eoner.baselib.widget.empty.FCPageStateView;
import com.eoner.baselib.widget.navgation.FCNavigationBar;
import com.eoner.baselibrary.bean.address.AddressBean;
import com.eoner.middlelib.router.map.FCRouterPath;
import com.masterclient.app.R;
import com.masterclient.app.base.ConfirmDialog;
import com.masterclient.app.base.FCBusinessActivity;
import com.masterclient.app.businessmodule.minemodule.address.adapter.ManageAddressAdapter;
import com.masterclient.app.businessmodule.minemodule.address.contract.ManageAddressContract;
import com.masterclient.app.businessmodule.minemodule.address.contract.ManageAddressPresenter;

import butterknife.BindView;
import butterknife.OnClick;
/**
 * 作者：sean
 * 创建时间：2017/7/31 20:04
 * 功能说明：
 */

@Route(path = FCRouterPath.MINE_ADDRESS_PATH)
public class ManageAddressActivity extends FCBusinessActivity<ManageAddressPresenter> implements ManageAddressContract.View{

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.tv_add_addre)
    TextView tvAddAddress;
    @BindView(R.id.empty_view)
    FCPageStateView emptyView;

    ManageAddressAdapter adapter;

    private ConfirmDialog confirmDialog;

    @Autowired(name = "isPurchase")
    String isPurchase = "0";
    @Autowired(name = "form")
    String form = "";

    @Override
    protected int requireGetLayoutId() {
        return R.layout.activity_manage_address;
    }

    @Override
    protected void requireSetCustomNavigationBar(FCNavigationBar navigationBar) {
        navigationBar.setTitle("地址管理");
    }

    @Override
    protected void requireInitUIAndData(Bundle savedInstanceState) {
        if (!TextUtils.isEmpty(form) && "dealer".equals(form)){
            mNavigationBar.setTitle("退货地址");
        }

        confirmDialog = new ConfirmDialog(mContext,"是否删除该地址？","取消","确定","1".equals(isPurchase)?"#FF5000":"#F23051");

        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new ManageAddressAdapter(isPurchase);
        recyclerView.setAdapter(adapter);
        emptyView.setViewState(FCPageViewState.PAGE_STATE_NO_DATA,R.mipmap.icon_empty_address,"留个地址，说不定有惊喜");
        TextView emptyBtn = emptyView.findViewById(R.id.tv_base_state_refresh);
        emptyBtn.setText("添加地址");
        emptyBtn.setVisibility(View.VISIBLE);
        emptyBtn.setOnClickListener(v->{
            startActivitry(AddNewAddressActivity.class,new String[][]{{"isPurchase", isPurchase}});
        });
        adapter.bindToRecyclerView(recyclerView);
        emptyView.setVisibility(View.GONE);
        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()){
                    case R.id.tv_edit:
                        Intent intent = new Intent(mContext,EditAddressActivity.class);
                        intent.putExtra("bean",mPresenter.list.get(position));
                        intent.putExtra("isPurchase",isPurchase);
                        startActivity(intent);
                        break;
                    case R.id.tv_delete:
                        confirmDialog.setOnChooseListerner(new ConfirmDialog.OnChooseListerner() {
                            @Override
                            public void cancel() {
                                confirmDialog.dismiss();
                            }
                            @Override
                            public void confirm() {
                                if(position < mPresenter.list.size()){
                                    mPresenter.deleteAddress(mPresenter.list.get(position).getSh_id());
                                }
                                confirmDialog.dismiss();
                            }
                        });
                        confirmDialog.show();
                        break;
                    case R.id.cb_check:
                        CheckBox cb = (CheckBox) view;
                        if(!cb.isChecked()){
                            cb.setChecked(true);
                        }
                        mPresenter.setAddressDefault(mPresenter.list.get(position).getSh_id());
                        break;
                }
            }
        });
    }

    @Override
    protected void onResume() {
        mPresenter.getAddressList();
        super.onResume();
    }

    @Override
    public void getAddressSuccess(AddressBean.DataBean dataBean) {
        mPresenter.list = dataBean.getSh_items();
        adapter.setNewData(dataBean.getSh_items());
//        if(adapter.getData().size() > 0){
        tvAddAddress.setVisibility(View.VISIBLE);
        if (mPresenter.list != null && mPresenter.list.size() > 0) {
            emptyView.setVisibility(View.GONE);
        } else {
            emptyView.setVisibility(View.VISIBLE);
        }
//        }else{
//            tvAddAddre.setVisibility(View.GONE);
//        }
    }

    @Override
    public void showDeleteSuccess() {
        showToast("删除地址成功");
        mPresenter.getAddressList();
    }

    @Override
    public void showDeleteFailed() {
        showToast("删除地址失败");
        mPresenter.getAddressList();
    }

    @Override
    public void changeDefaultSuccess() {
        mPresenter.getAddressList();
    }

    @Override
    public void changeDefaultfailed() {
        showToast("设置默认地址失败");
        mPresenter.getAddressList();
    }

    @Override
    public void baseShowError() {
        super.baseShowError();
        emptyView.setVisibility(View.GONE);
    }

    @Override
    public void onClickPageStateErrorView() {
        super.onClickPageStateErrorView();
        mPresenter.getAddressList();
    }

    @OnClick({R.id.tv_add_addre}) void onClick(View view){
        switch (view.getId()){
            case R.id.tv_add_addre:
                startActivitry(AddNewAddressActivity.class,new String[][]{{"isPurchase", isPurchase}});
                break;
        }
    }
}
